import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class Web_communication {


    public static void sendHttpRequestByGET(String urlString) {
        try {
            // 创建 URL 对象
            URL url = new URL(urlString);
            // 打开与 URL 的连接，并进行类型转换
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            // 设置请求方法为 GET
            connection.setRequestMethod("GET");

            // 获取响应码
            int responseCode = connection.getResponseCode();
            // 打印响应码
            System.out.println("Response Code: " + responseCode);

            // 创建用于读取输入流的 BufferedReader
            BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            String line;
            StringBuilder response = new StringBuilder();
            // 逐行读取响应内容并添加到 StringBuilder 中
            while ((line = reader.readLine())!= null) {
                response.append(line);
            }
            reader.close();
            // 打印响应内容
            System.out.println("Response: " + response.toString());

        } catch (IOException e) {
            // 打印可能出现的异常信息
            e.printStackTrace();
        }
    }
}
